from flask import Blueprint ,request
import random
from .register import SaveCodes
from account_numbers.update_password import Update

app_account = Blueprint('app_account',__name__) 


@app_account.route('/verify',methods=['GET'])
def register():
    '''在这里实现把用户输进来的电话和系统生成的验证码保存到数据库'''
    info = request.args
    phone=info.get('phone')
    verify_code = random.randint(100000,999999)
    with SaveCodes()as SC:
        SC.save_codes(phone,verify_code)
    return SC.send_masg()
@app_account.route('/register',methods=['POST'])
def registes():
    '''在这个视图函数实现用户注册的过程'''
    info = request.form
    phone = info.get('phone')
    verify_code = info.get('verify_code')
    pass_word = info.get('pass_word')
    with SaveCodes()as SC:
        result = SC.register(phone,verify_code,pass_word)
    return result

@app_account.route('/code',methods=['POST'])
def updates():
    info = request.form
    phone=info.get('phone')    
    verify_code = random.randint(100000,999999)
    with SaveCodes()as L:
        L.save_codes(phone,verify_code)
    return L.send_masg()

@app_account.route('/update',methods=['POST'])
def update_verify():
    info = request.form
    phone=info.get('phone')
    password = info.get('pass_word')
    phone_verify_code = info.get('phone_verify_code')
    with Update()as U:
        result = U.update_pwd(phone,phone_verify_code,password)
    return result 


